//layui图片上传模块
layui.use(['upload'], function () {
    var layer = layui.layer;
    var $ = layui.jquery
        , upload = layui.upload;
    //普通图片上传开始
    var uploadInst = upload.render({
        elem: '#upload_img',
        url: '/foodshare/main/upload',
        accept:'images',
        size: 500, //限制文件大小，单位 KB
        before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#upload_img').attr('src', result); //图片链接（base64）
            });
        },
        done: function (res) {
            //如果上传失败
            if (res.status > 0) {
                return layer.msg('上传失败');
            }
            //上传成功
            var demoText = $('#demoText');
            demoText.html('<span style="color: #4cae4c;">上传成功</span>');

            var fileupload = $(".image");
            //获取后端传过来的图片的url,之后给表单的隐藏图片输入框赋值
            fileupload.attr("value",res.data.src);
            console.log(fileupload.attr("value"));
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });
    //普通图片上传结束
})